﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseWebAPP.code.DAL;
using WarehouseWebAPP.code.DAL.LendItemDSTableAdapters;


namespace WarehouseWebAPP.code.BLL
{
    public class LendItemBLL
    {
        lend_itemTableAdapter LendItemAdapter = null;
        public lend_itemTableAdapter Adapter
        {
            get
            {
                if (LendItemAdapter == null) LendItemAdapter = new lend_itemTableAdapter();
                return LendItemAdapter;
            }
        }

        public void LendItemInsert(string lend_item_serial, Guid lend_item_vendor, string lend_item_description, string lend_item_place, Guid lend_item_supplier)
        {
            if (lend_item_serial == null)
                throw new Exception("Item serial is missing");
            if (Adapter.LendItemInsert(lend_item_serial,  lend_item_description, lend_item_place,lend_item_supplier,lend_item_vendor) < 1)
                throw new Exception("Item was not Inserted");
            //log event
            string[] Parameters = { lend_item_serial, "Item" };
            MyEvent ev = History.EventDecorator(History.ActionType.Add, Parameters, DateTime.Now);
            History.AddEvent(ev);

        }

        public void LendItemUpdate(string lend_item_serial, Guid lend_item_vendor, string lend_item_description, string lend_item_place, Guid lend_item_supplier,bool lend_item_available, Guid lend_item_num)
        {
            if (lend_item_num == null)
                throw new Exception("Item number is missing");
            if (Adapter.LendItemUpdate(lend_item_serial, lend_item_vendor, lend_item_description, lend_item_place, lend_item_supplier,lend_item_available, lend_item_num) < 1)
                throw new Exception("Item was not updated");

            //log event
            string[] Parameters = { lend_item_serial, "Item" };
            MyEvent ev = History.EventDecorator(History.ActionType.Update, Parameters, DateTime.Now);
            History.AddEvent(ev);
        }

        public void LendItemDelete(Guid lend_item_num)
        {
            if (lend_item_num == null)
                throw new Exception("Item number is missing");
            string lend_item_serial = LendItemByNum(lend_item_num)[0].lend_item_serial;
            if (Adapter.LendItemDelete(lend_item_num) < 1)
                throw new Exception("Item was not deleted");

            //log event
            string[] Parameters = { lend_item_serial, "Item" };
            MyEvent ev = History.EventDecorator(History.ActionType.Delete, Parameters, DateTime.Now);
            History.AddEvent(ev);
        }

        public LendItemDS.lend_itemDataTable LendItemGetAvailable()
        {
            return Adapter.LendItemGetAvailable();
        }

        public LendItemDS.lend_itemDataTable LendItemGetUnavailable()
        {
            return Adapter.LendItemGetUnavailable();
        }


        public void LendItemSetAvailable(Guid lend_item_num)
        {
            if (lend_item_num == null)
                throw new Exception("Item number is missing");
            if (Adapter.LendItemSetAvailable(lend_item_num)<1)
               throw new Exception("error in LendItemSetAvailable has occured");
        }

        public void LendItemSetUnavailable(Guid lend_item_num)
        {
            if (lend_item_num == null)
                throw new Exception("Item number is missing");
            Adapter.LendItemSetUnavailable(lend_item_num);

        }

        public LendItemDS.lend_itemDataTable LendItemGetByPlace(string lend_item_place)
        {
            if (lend_item_place == null)
                throw new Exception("Item Place is missing");
            return Adapter.LendItemGetByPlace(lend_item_place);
        }

        public LendItemDS.lend_itemDataTable LendItemBySerial(string lend_item_serial)
        {
            if (lend_item_serial == null)
                throw new Exception("Item Serial is missing");
            return Adapter.LendItemGetBySerial(lend_item_serial);
        }

        public LendItemDS.lend_itemDataTable LendItemByNum(Guid lend_item_num)
        {
            if (lend_item_num == null)
                throw new Exception("Item Serial is missing");
            return Adapter.LendItemGetByNum(lend_item_num);
        }

        public LendItemDS.lend_itemDataTable lendItemGetByVendor(Guid lend_item_vendor)
        {
            if (lend_item_vendor == null)
                throw new Exception("Item vendor is missing");
            return Adapter.LendItemGetByVendor(lend_item_vendor);

        }

        public LendItemDS.lend_itemDataTable lendItemGetBySupplier(Guid lend_item_supplier)
        {
            if (lend_item_supplier == null)
                throw new Exception("Item supplier is missing");
            return Adapter.LendItemGetBySupplier(lend_item_supplier);

        }
        public LendItemDS.lend_itemDataTable lendItemGet()
        {
            return Adapter.GetData();
        }

        public LendItemDS.lend_itemDataTable LendItemSearch(string lend_item_serial, string lend_item_vendor, string lend_item_description, string lend_item_place, string lend_item_supplier, Boolean lend_item_available)
        {
            return Adapter.LendItemSearch(lend_item_serial, lend_item_vendor, lend_item_supplier, lend_item_description, lend_item_place, lend_item_available);
            
        }



                



    }
}
